/*
 * 1.3
 * Design an algorithm and write code to remove the duplicate characters in a string
 * without using any additional buffer. NOTE: One or two additional variables are fine.
 * An extra copy of the array is not.
 * FOLLOW UP
 * Write the test cases for this method.
 */
#include <stdio.h>
#include <string.h>

// XXX: totally misunderstood!!!
void remove_duplicates_old(char *str)
{
    int i = 0, j;
    int len = strlen(str);
    while (i < len && str[i+1] != str[i]) { i++; }
    if (i == len-1) { return; }
    i++;
    j = i;
    do {
        char start = str[j];
        while (str[++j] == start) { }
        str[i++] = str[j];
    } while (j < len);
}

void remove_duplicates(char *str)
{
    char elems[255] = { 0 };
    int end = 0;
    for (int i = 0; i < strlen(str); i++) {
        if (!elems[str[i]]) {
            str[end++] = str[i];
            elems[str[i]] = 1;
        }
    }
    str[end] = '\0';
}

int main()
{
    char s[] = "a";
    remove_duplicates(s);
    printf("%s\n", s);
    char s2[] = "abbbcb";
    remove_duplicates(s2);
    printf("%s\n", s2);
    char s3[] = "aabbccac";
    remove_duplicates(s3);
    printf("%s\n", s3);
}
